import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


public class Chart 
{
	HashSet<Sign>[][] theChart;
	int numWords;
	
	
	/*Constructs chart based on number of words in sentence*/
	public Chart(int numWords)
	{
		theChart = new HashSet[numWords][numWords];
		for(int a = 0; a < numWords; a++)
		{
			for(int b = 0; b < numWords; b++)
			{
				theChart[a][b] = new HashSet<Sign>();
			}
		}
		
		
		this.numWords = numWords;
	}
	
	public void addToChart(Sign s, int start, int end)
	{
		boolean foundIt = false;
		for(Sign spotlight: theChart[start][end])
		{
			if(s.hashCode() == spotlight.hashCode())
			{
				if(s.equals(spotlight))
				{
					if(s.getComplexity() > spotlight.getComplexity())
					{
						foundIt = true;
					//	System.out.println("redundant");
					}
					else
					{
					//	System.out.println("added less complex");
						theChart[start][end].remove(spotlight);
						break;		
					}
				}
			}
			if(foundIt)
				break;
		}
		if(!foundIt)
		{
			//System.out.println("added new");
			theChart[start][end].add(s);
		}
	}
	
	/*Gets the HashSet of signs covering a given stretch*/
	public HashSet<Sign> getSigns(int start, int end)
	{
		return theChart[start][end];
	}
	
	public HashSet<Sign> getResults()
	{
		return theChart[0][numWords-1];
	}
	
	public String toString()
	{
		String result = "";
		for(int a= 0; a < numWords; a++)
		{
			for(int b = 0; b < numWords; b++)
			{
				HashSet<Sign> theStuff = theChart[a][b];
				Iterator<Sign> i = theStuff.iterator();
				while(i.hasNext())
				{
					Sign spotlight = i.next();
					result+= a + " " + b + ": " + spotlight.toStringReadable() + "\n";
				}
			}
		}
		return result;
	}
}
